﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using Travel.BusinessLogic.Hotels;

namespace Travel
{
    public partial class TravelForm : Form
    {
        public TravelForm()
        {
            InitializeComponent();
            initializeHotelComponents();
        }

        private void initializeHotelComponents()
        {
            try
            {
                comboBox2.Items.Add(new HotelProxy().getHotel());
                comboBox2.SelectedIndex = 0;

                int?[] rooms = new HotelProxy().getRooms();
                Console.WriteLine(rooms.Length);
                for (int i = 0; i < rooms.Length; i++)
                {
                    comboBox3.Items.Add("Room " + rooms[i]);
                }
                comboBox3.SelectedIndex = 0;
            }
            catch (Exception e)
            {
                //If the webservice is anavailable, we do not set anything
            }
        }

        eTravel travel;
        eTicket ticket;

        private void TravelForm_Load(object sender, EventArgs e)
        {
            tbSeat.DataBindings.Clear();
            if (seat != null)
            {
                tbSeat.DataBindings.Add(new Binding("Text", seat, "Descriptor"));
            }
            travel = new eTravel();
            rbFlighAndHotel.Checked = true;            

        }

        private void onAnyChange(object sender, CancelEventArgs e)
        {

        }

        private void btnGetFlights_Click(object sender, EventArgs e)
        {
            string departureCity = tbDepartureCity.Text;
            string arrivalCity = tbArrivalCity.Text;
            DateTime departureDate = dtpDeparture.Value;
            List<eFlight> flights = FlightProxy.GetFlights(departureCity, arrivalCity, departureDate);
            cbFlights.DataSource = flights;
            cbFlights.DisplayMember = "Descriptor";

        }

        eFlight flight;
        eSeat seat;

        private void btnChangeSeat_Click(object sender, EventArgs e)
        {
            SeatsForm f = new SeatsForm();
            string flightCode = ((eFlight)cbFlights.SelectedItem).Code;
            List<eSeat> seats = FlightProxy.GetSeats(flightCode);
            f.lbSeats.DataSource = seats;
            f.lbSeats.DisplayMember = "Descriptor";
            f.ShowDialog();
            seat = (eSeat)f.lbSeats.SelectedItem;
            tbSeat.DataBindings.Clear();
            tbSeat.DataBindings.Add(new Binding("Text", seat, "Descriptor"));

        }

        private void cbFlights_SelectedIndexChanged(object sender, EventArgs e)
        {
            eFlight newFlight = (eFlight)cbFlights.SelectedItem;
            if (newFlight != flight)
            {
                flight = newFlight;
                seat = null;
                tbSeat.Text = "";
                dateTimePicker2.Value = flight.Arrival;
            }


        }

        private void rbFlight_CheckedChanged(object sender, EventArgs e)
        {
            gbFlight.Enabled = true;
            gbHotel.Enabled = false;
        }

        private void rbFlighAndHotel_CheckedChanged(object sender, EventArgs e)
        {
            gbFlight.Enabled = true;
            gbHotel.Enabled = true;
        }

        private void rbHotel_CheckedChanged(object sender, EventArgs e)
        {
            gbFlight.Enabled = false;
            gbHotel.Enabled = true;
        }

        private void tbArrivalCity_Validated(object sender, EventArgs e)
        {
            textBox4.Text = tbArrivalCity.Text;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (rbFlighAndHotel.Checked || rbFlight.Checked)
            {
                eTicket t = new eTicket(flight, seat, tbPassegenerName.Text);
            }

            if (rbFlighAndHotel.Checked || rbHotel.Checked)
            {
                if (checkBox1.Checked)
                {
                    eRoomRental r = new eRoomRental(dateTimePicker2.Value, textBox3.Text, tbPassegenerName.Text);
                }
                else
                {
                    eRoomRental r = new eRoomRental(dateTimePicker2.Value, textBox3.Text, (comboBox3.SelectedIndex), tbPassegenerName.Text);
                }
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                comboBox3.Enabled = false;
            }
            else
            {
                comboBox3.Enabled = true;
            }
        }
    }

}
